package com.ruoyi.course.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.course.domain.*;
import com.ruoyi.course.mapper.*;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.service.ICoursesSelectedService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;
import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 已选课程Service业务层处理
 *
 * @author mac
 * @date 2023-03-11
 */
@Service public class CoursesSelectedServiceImpl implements ICoursesSelectedService {
    @Autowired private CoursesSelectedMapper coursesSelectedMapper;
    @Autowired private SysUserMapper userMapper;
    @Autowired private CourseClassMapper courseClassMapper;
    @Autowired private CourseFloorMapper courseFloorMapper;
    @Autowired private CoursePartMapper coursePartMapper;
    @Autowired private SysUserRoleMapper userRoleMapper;
    @Autowired private CourseAllMapper courseAllMapper;
    @Autowired private CourseBanMapper courseBanMapper;

    /**
     * 查询已选课程
     *
     * @param id 已选课程主键
     * @return 已选课程
     */
    @Override public CoursesSelected selectCoursesSelectedById(Long id) {
        return coursesSelectedMapper.selectCoursesSelectedById(id);
    }

    /**
     * 查询已选课程列表
     *
     * @param coursesSelected 已选课程
     * @return 已选课程
     */
    @Override public List<CoursesSelected> selectCoursesSelectedList(CoursesSelected coursesSelected) {
        List<SysRole> roles = getLoginUser().getUser().getRoles();
        for (SysRole role : roles) {
            if (role.getRoleId() == 2) {
                String className = getLoginUser().getUser().getClassName();
                if (StringUtils.isEmpty(className)) {
                    throw new RuntimeException("当前用户无班级信息，请联系管理员分配所在班级！");
                }
                coursesSelected.setMemo(className);
                return coursesSelectedMapper.selectCoursesSelectedList(coursesSelected);
            }
        }

        return coursesSelectedMapper.selectCoursesSelectedList(coursesSelected);
    }

    /**
     * 新增已选课程
     *
     * @param coursesSelected 已选课程
     * @return 结果
     */
    @Override public int insertCoursesSelected(CoursesSelected coursesSelected) {
        coursesSelected.setCreateTime(DateUtils.getNowDate());
        return coursesSelectedMapper.insertCoursesSelected(coursesSelected);
    }

    /**
     * 修改已选课程
     *
     * @param coursesSelected 已选课程
     * @return 结果
     */
    @Override public int updateCoursesSelected(CoursesSelected coursesSelected) {
        return coursesSelectedMapper.updateCoursesSelected(coursesSelected);
    }

    /**
     * 批量删除已选课程
     *
     * @param ids 需要删除的已选课程主键
     * @return 结果
     */
    @Override public int deleteCoursesSelectedByIds(Long[] ids) {
        return coursesSelectedMapper.deleteCoursesSelectedByIds(ids);
    }

    /**
     * 删除已选课程信息
     *
     * @param id 已选课程主键
     * @return 结果
     */
    @Override public int deleteCoursesSelectedById(Long id) {
        return coursesSelectedMapper.deleteCoursesSelectedById(id);
    }

    public static class CourseTeacher {
        private String courseName;
        private String teacherName;

        public String getCourseName() {
            return courseName;
        }

        public void setCourseName(String courseName) {
            this.courseName = courseName;
        }

        public String getTeacherName() {
            return teacherName;
        }

        public void setTeacherName(String teacherName) {
            this.teacherName = teacherName;
        }
    }

    public static class FloorClass {
        private String floorName;
        private String className;

        public String getFloorName() {
            return floorName;
        }

        public void setFloorName(String floorName) {
            this.floorName = floorName;
        }

        public String getClassName() {
            return className;
        }

        public void setClassName(String className) {
            this.className = className;
        }
    }

    @Override public int oneClickScheduling() {

        List<String> 生物系 = new ArrayList<>();
        // 所有课程即对应的老师
        List<CourseTeacher> courseAllList = new ArrayList<>();
        courseAllMapper.selectCourseAllList(null).forEach(t -> {
            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.setCourseName(t.getCourseName());
            courseTeacher.setTeacherName(t.getCourseTeacher());
            courseAllList.add(courseTeacher);
        });

        // 所有楼栋、所有教室
        List<FloorClass> courseClassList = new ArrayList<>();
        List<CourseClass> courseClasses = courseClassMapper.selectCourseClassList(null);
        courseClasses.forEach(t -> {
            FloorClass floorClass = new FloorClass();
            CourseFloor floor = courseFloorMapper.selectCourseFloorById(t.getFloorId());
            floorClass.setFloorName(floor.getFloorName());
            floorClass.setClassName(t.getClassName());

            courseClassList.add(floorClass);
        });

        // 所有院系
        List<String> coursePartList = new ArrayList<>();
        List<CoursePart> courseParts = coursePartMapper.selectCoursePartList(null);
        courseParts.forEach(t -> {
            coursePartList.add(t.getPartName());
        });

        // 所有班级
        List<String> courseBanList = new ArrayList<>();
        List<CourseBan> courseBans = courseBanMapper.selectCourseBanList(null);
        courseBans.forEach(t -> {
            courseBanList.add(t.getBanName());
        });

        int courseAllSize = courseAllList.size();
        List<String> weeks = Arrays.asList("星期一", "星期二", "星期三", "星期四", "星期五");
        List<String> hours = Arrays.asList("8:00", "10:00", "12:00", "14:00", "16:00", "18:00");
        Random random = new Random();

        // 课程老师遍历
        for (int i = 0; i < courseAllSize; i++) {
            insetCourseSelected(courseAllList, random, weeks, hours, courseClassList, courseBanList);
        }

        // 院系遍历
        for (int i = 0; i < coursePartList.size(); i++) {
            insetCourseSelected(courseAllList, random, weeks, hours, courseClassList, courseBanList);
        }

        // 楼栋遍历 教室遍历
        for (int i = 0; i < courseClassList.size(); i++) {
            insetCourseSelected(courseAllList, random, weeks, hours, courseClassList, courseBanList);
        }

        // 班级遍历
        for (int i = 0; i < courseBanList.size(); i++) {
            insetCourseSelected(courseAllList, random, weeks, hours, courseClassList, courseBanList);
        }

        return 0;
    }

    private void insetCourseSelected(List<CourseTeacher> courseAllList, Random random, List<String> weeks,
        List<String> hours, List<FloorClass> courseClassList, List<String> courseBanList) {
        CoursesSelected selected = new CoursesSelected();
        int courseNameSize = courseAllList.size();
        CourseTeacher courseTeacher = courseAllList.get(random.nextInt(courseNameSize));
        selected.setCourseName(courseTeacher.getCourseName());
        selected.setCourseTeacher(courseTeacher.getTeacherName());
        int hour = random.nextInt(5);
        selected.setClassTime(weeks.get(random.nextInt(5)) + hours.get(hour) + "~" + hours.get(hour + 1));

        FloorClass floorClass = courseClassList.get(random.nextInt(courseClassList.size()));

        String floorName = floorClass.getFloorName();
        String className = floorClass.getClassName();
        String classAddress = floorName + className;
        selected.setClassAddress(classAddress);

        String banName = courseBanList.get(random.nextInt(courseBanList.size()));
        selected.setMemo(banName);

        CoursesSelected entity = coursesSelectedMapper.selectCoursesSelectedByParam(selected);
        if (entity != null) {
            return;
        }

        CourseBan courseBan = courseBanMapper.selectCoursePartByBanName(selected.getMemo());
        if (Objects.isNull(courseBan)){
            return;
        }
        CoursePart coursePart = coursePartMapper.selectCoursePartByName(courseBan.getPartName());
        if (Objects.isNull(coursePart)) {
            return;
        }

        CourseAll courseAll = new CourseAll();
        courseAll.setCourseName(selected.getCourseName());
        courseAll.setCourseTeacher(selected.getCourseTeacher());
        courseAll.setCoursePartId(coursePart.getId());
        courseAll.setCoursePart(coursePart.getPartName());
        CourseAll all = courseAllMapper.selectCourseAllByCourseAll(courseAll);
        if (Objects.isNull(all)) {
            return;
        }
        coursesSelectedMapper.insertCoursesSelected(selected);
    }
}
